package com.seqb.manage.user.controller;

import com.github.pagehelper.Page;
import com.seqb.annotation.Log;
import com.seqb.common.Query;
import com.seqb.common.ResultVo;
import com.seqb.common.controller.BaseController;
import com.seqb.manage.role.domain.RoleDO;
import com.seqb.manage.role.service.RoleService;
import com.seqb.manage.user.domain.UserDO;
import com.seqb.manage.user.service.UserService;
import com.seqb.utils.PageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author lilinshen
 * @title 用户账号表
 * @description 请填写相关描述
 * @date 2019/11/15
 */
@Controller
@RequestMapping("/manage/user/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @GetMapping()
    @RequiresPermissions("manage:user:user:list")
    @Log("用户账号表列表")
    String user() {
        return "manage/user/user.list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("manage:user:user:list")
    @Log("用户账号表列表")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<UserDO> page = userService.list(query);
        PageUtils pageUtils = new PageUtils(page.getResult(), page.getTotal());
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("manage:user:user:add")
    @Log("用户账号表添加")
    String add(Model model) {
        List<RoleDO> roles = roleService.listUserRole(null);
        model.addAttribute("roles", roles);
        return "manage/user/user.add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("manage:user:user:edit")
    @Log("用户账号表编辑")
    String edit(@PathVariable("id") Long id, Model model) {
        UserDO user = userService.get(id);
        model.addAttribute("user", user);
        List<RoleDO> roles = roleService.listUserRole(id);
        model.addAttribute("roles", roles);
        return "manage/user/user.edit";
    }

    /**
     * 添加保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("manage:user:user:add")
    @Log("用户账号表添加保存")
    public ResultVo save(UserDO user) {
        return userService.save(user);
    }

    /**
     * 编辑保存
     */
    @ResponseBody
    @RequestMapping("/update")
    @Log("用户账号表编辑保存")
    @RequiresPermissions("manage:user:user:edit")
    public ResultVo update(UserDO user) {
        return userService.update(user);
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("manage:user:user:remove")
    @Log("用户账号表删除")
    public ResultVo remove(Long id) {
        if (userService.remove(id) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("manage:user:user:remove")
    @Log("用户账号表批量删除")
    public ResultVo remove(@RequestParam("ids[]") Long[] ids) {
        userService.batchRemove(ids);
        return ResultVo.ok();
    }

}
